package com.lingo.ffmpegdemo.lg.iodemo.util;

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;
import java.util.List;
import java.util.Map;

/**
 * @author watson
 * @date 2021/6/15 18:00
 * @Description: TODO
 **/
public class ExcelUtils {


    //向Excel中写数据
    public static void writeExcel(List<Map<String, String>> list, String filePath) {
        filePath = "C:\\Users\\Administrator\\Desktop\\work\\hk_kafka-data\\" + filePath + ".xlsx";
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet();
        XSSFRow firstRow = sheet.createRow(0);//第一行表头
        XSSFCell cells[] = new XSSFCell[4];

        String[] titles = new String[]{"摄像头编号", "时间", "车牌号", "车道号"};
        //循环设置表头信息
        for (int i = 0; i < 4; i++) {
            cells[0] = firstRow.createCell(i);
            cells[0].setCellValue(titles[i]);
        }

        //遍历list,将数据写入Excel中
        for (int i = 0; i < list.size(); i++) {
            XSSFRow row = sheet.createRow(i + 1);
//            HSSFRow row = sheet.createRow(i + 1);
            Map<String, String> fsd = list.get(i);
            XSSFCell cell = row.createCell(0);
//            cell.setCellValue(new HSSFRichTextString(fsd.get("cameraIndexCode")));
            cell.setCellValue(fsd.get("cameraIndexCode"));

            cell = row.createCell(1);
            cell.setCellValue(fsd.get("captureTime"));

            cell = row.createCell(2);
            cell.setCellValue(fsd.get("plateNo"));

            cell = row.createCell(3);
            cell.setCellValue(fsd.get("laneNo"));
        }
        OutputStream out = null;
        try {
            out = new FileOutputStream(filePath);
            workbook.write(out);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void appendWriter(List<Map<String, String>> list, String filePath) {
        filePath = "C:\\Users\\Administrator\\Desktop\\work\\hk_kafka-data\\" + filePath + ".xlsx";
        try {
            FileInputStream in = new FileInputStream(filePath);
            XSSFWorkbook workbook = new XSSFWorkbook(in);
            XSSFSheet sheet = workbook.getSheetAt(0);
            XSSFRow row = sheet.getRow(1);

            FileOutputStream out = new FileOutputStream(filePath);
            //从第二行开始追加列
        /*row=sheet.getRow(1);
        row.createCell(3).setCellValue("AAA");
        row.createCell(4).setCellValue("BBB");*/

            //追加列数据
//            for (int i = 0; i < stuList2.size(); i++) {
//                Student student = stuList2.get(i);
//                row = sheet.getRow(i + 1);
//                row.createCell(3).setCellValue(student.getAge());
//                row.createCell(4).setCellValue(student.getName());
//                row.createCell(5).setCellValue(student.getAddress());
//            }

            //在现有行号后追加数据
            int rowNum = sheet.getLastRowNum();
            for (int i = 0; i < list.size(); i++) {
                rowNum++;
                row = sheet.createRow(rowNum);
                //追加行数据
                Map<String, String> fsd = list.get(i);
                XSSFCell cell = row.createCell(0);
                cell.setCellValue(fsd.get("cameraIndexCode"));

                cell = row.createCell(1);
                cell.setCellValue(fsd.get("captureTime"));

                cell = row.createCell(2);
                cell.setCellValue(fsd.get("plateNo"));

                cell = row.createCell(3);
                cell.setCellValue(fsd.get("laneNo"));
            }
            out.flush();
            workbook.write(out);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 导出数据生成EXCEL方法
     *
     * @param list
     * @throws IOException
     */
    public static HSSFWorkbook exportExcelBook(List<Map<String, String>> list)
            throws IOException {

        //文件名称，客户端传来的参数，防止中文文件名乱码参数编码因此这里需要解码
        //创建Excel工作薄对象
        HSSFWorkbook workbook = new HSSFWorkbook();
        //创建Excel工作表对象
        HSSFSheet sheet = workbook.createSheet();
        sheet.setColumnWidth(0, 3000);
        sheet.setColumnWidth(1, 3000);
        sheet.setColumnWidth(2, 3000);
        sheet.setColumnWidth(3, 3000);

//        // 设置表头字体样式
//        HSSFFont columnHeadFont = workbook.createFont();
//        columnHeadFont.setFontName("宋体");
//        columnHeadFont.setFontHeightInPoints((short) 10);
//        columnHeadFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//
//        // 列头的样式
//        HSSFCellStyle columnHeadStyle = workbook.createCellStyle();
//        columnHeadStyle.setFont(columnHeadFont);
//        // 左右居中
//        columnHeadStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//        // 上下居中
//        columnHeadStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
//        columnHeadStyle.setLocked(true);
//        columnHeadStyle.setWrapText(true);
//        // 左边框的颜色
//        columnHeadStyle.setLeftBorderColor(HSSFColor.BLACK.index);
//        // 边框的大小
//        columnHeadStyle.setBorderLeft((short) 1);
//        // 右边框的颜色
//        columnHeadStyle.setRightBorderColor(HSSFColor.BLACK.index);
//        // 边框的大小
//        columnHeadStyle.setBorderRight((short) 1);
//        // 设置单元格的边框为粗体
//        columnHeadStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
//        // 设置单元格的边框颜色
//        columnHeadStyle.setBottomBorderColor(HSSFColor.BLACK.index);
//        // 设置单元格的背景颜色（单元格的样式会覆盖列或行的样式）
//        columnHeadStyle.setFillForegroundColor(HSSFColor.WHITE.index);
//        // 设置普通单元格字体样式
//        HSSFFont font = workbook.createFont();
//        font.setFontName("宋体");
//        font.setFontHeightInPoints((short) 10);

        //创建Excel工作表第一行
        HSSFRow row0 = sheet.createRow(0);
        // 设置行高
        row0.setHeight((short) 750);
        HSSFCell cell = row0.createCell(0);
        //设置单元格内容
        cell.setCellValue(new HSSFRichTextString("摄像头"));
        //设置单元格字体样式
//        cell.setCellStyle(columnHeadStyle);
        cell = row0.createCell(1);
        cell.setCellValue(new HSSFRichTextString("时间"));
//        cell.setCellStyle(columnHeadStyle);
        cell = row0.createCell(2);
        cell.setCellValue(new HSSFRichTextString("车道号"));
//        cell.setCellStyle(columnHeadStyle);
        cell = row0.createCell(3);
        cell.setCellValue(new HSSFRichTextString("车牌号"));
//        cell.setCellStyle(columnHeadStyle);

        // 循环写入数据
        for (int i = 0; i < list.size(); i++) {
            Map<String, String> fsd = list.get(i);
            HSSFRow row = sheet.createRow(i + 1);
            cell = row.createCell(0);
            cell.setCellValue(new HSSFRichTextString(fsd.get("cameraIndexCode")));
//            cell.setCellStyle(columnHeadStyle);

            cell = row.createCell(1);
            cell.setCellValue(new HSSFRichTextString(fsd.get("captureTime")));
//            cell.setCellStyle(columnHeadStyle);

            cell = row.createCell(2);
            cell.setCellValue(new HSSFRichTextString(fsd.get("plateNo")));
//            cell.setCellStyle(columnHeadStyle);

            cell = row.createCell(3);
            cell.setCellValue(new HSSFRichTextString(fsd.get("laneNo")));
//            cell.setCellStyle(columnHeadStyle);
        }
        return workbook;
    }
}
